import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store/index'
import utils from '@/config/cookieUtils'
// import Index from '@/pages/Index'
import Error from '@/pages/Error'
import SchoolNumber from '@/pages/SchoolNumber'
import UserNumber from '@/pages/UserNumber'
import TwIndex from '@/subpages/Index/TwIndex'
import ApplicationMonitoring from '@/subpages/Index/ApplicationMonitoring'
import APIMonitoring from '@/subpages/Index/APIMonitoring'
import DataMonitoring from '@/subpages/Index/DataMonitoring'
import ServiceMonitoring from '@/subpages/Index/ServiceMonitoring'
import TeachingMonitoring from '@/subpages/Index/TeachingMonitoring'
import ResourceMonitoring from '@/subpages/Index/ResourceMonitoring'
import EnrollmentInformation from '@/subpages/Index/EnrollmentInformation'
import School from '@/subpages/Index/School'
import Teacher from '@/subpages/Index/Teacher'
import Student from '@/subpages/Index/Student'
import Forecase from '@/subpages/Index/Forecase/Forecase.vue'
import Graduate from '@/subpages/Index/Graduate/Graduate.vue'
import Service from '@/subpages/Index/Service'
import Reading from '@/subpages/Index/reading.vue'

Vue.use(Router)

var router = new Router({
  routes: [
    { path: '/', redirect: '/index/main' },
    { path: '/index',
      redirect: '/index/main',
      component: () => import(/* webpackChunkName: "student" */ '@/pages/Index'),
      children: [
        {
          path: 'main',
          component: TwIndex
        },
        {
          path: 'applicationmonitoring',
          component: ApplicationMonitoring
        },
        {
          path: 'apimonitoring',
          component: APIMonitoring
        },
        {
          path: 'datamonitoring',
          component: DataMonitoring
        },
        {
          path: 'servicemonitoring',
          component: ServiceMonitoring
        },
        {
          path: 'teachingmonitoring',
          component: TeachingMonitoring
        },
        {
          path: 'resourcemonitoring',
          component: ResourceMonitoring
        },
        {
          path: 'enrollmentinformation',
          component: EnrollmentInformation
        },
        {
          path: 'schoolmonitoring',
          component: School
        },
        {
          path: 'teachermonitoring',
          component: Teacher
        },
        {
          path: 'studentmonitoring',
          component: Student
        },
        {
          path: 'forecasemonitoring',
          component: Graduate
        },
        {
          path: 'servicesmonitoring',
          component: Service
        },
        {
          path: 'forecase',
          component: Forecase
        },
        {
          path: 'reading',
          component: Reading
        }
        // {
        //   path: 'graduate',
        //   name: 'graduate',
        //   component: () => import(/* webpackChunkName: "graduate" */ '../subpages/Graduate/index')
        // }
      ]
    },
    { path: '/schoolnumber', component: SchoolNumber },
    { path: '/usernumber', component: UserNumber },
    {
      path: '/sslogin',
      component: () => import('@/pages/sso/ssoLogin.vue')
    },
    {
      // 404 页面配置,无法匹配的路由均跳转至错误页面
      path: '*',
      component: Error,
      name: 'error',
      meta: {
        title: '页面没找到'
      }
    }
  ]
})

router.beforeEach((to, from, next) => {
  var userId = utils.get('jczc_curr_userid')
  var storeUserId = store.state.user.userId

  // 存在userId说明用户已登录
  if (userId) {
    // 判断vuex中的userId与cookie中的userId是否相等，以避免重复设置vuex
    if (storeUserId !== userId) {
      store.commit('setUser', {
        userId: userId
      })
    }
  }

  next()
})
//
// router.afterEach((to, from, next) => {
//
// })
export default router
